<template>
  <d2-container>
    <div>
      <el-form :inline="true" class="demo-form-inline">
        <el-form-item>
          <el-select size="small" v-model="statusVal" placeholder="请选择审核状态">
            <el-option v-for="item in statuslist" :key="item.value" :label="item.label" :value="item.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-input clearable placeholder="请输入订单号" size="small" v-model="searchList.searchValue"></el-input>
        </el-form-item>
        <el-form-item>
          <el-date-picker size="small" v-model="searchList.timelist" type="daterange" align="right" unlink-panels range-separator="至"
                          start-placeholder="开始日期" end-placeholder="结束日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" size="mini" @click="init">搜索</el-button>
        </el-form-item>
      </el-form>
      <el-table v-loading="loading" border :data="tableData" style="width: 100%" header-align="center">
        <el-table-column type="index" label="序号" width="80" align="center"></el-table-column>
        <el-table-column prop="task_order" label="订单编号" align="center"></el-table-column>
        <el-table-column prop="task_shopname" label="店铺名" align="center"></el-table-column>
        <el-table-column prop="brushhands_tbname" label="刷手会员号" width="180" align="center"></el-table-column>
        <el-table-column prop="baby_id" label="宝贝ID" width="200" align="center"></el-table-column>
        <el-table-column prop="task_bjmoney" label="商品金额" width="120" align="center"></el-table-column>
        <el-table-column prop="create_time" label="创建时间" align="center"></el-table-column>
        <el-table-column fixed="right" label="审核" width="160" align="center">
          <template slot-scope="scope">
            <div v-if="scope.row.status == 1">
              <el-button @click="onPass(scope.row,1)" type="primary" plain size="mini">通过</el-button>
              <el-button @click="onPass(scope.row,2)" size="mini" type="danger" plain>拒绝</el-button>
            </div>
            <el-button v-if="scope.row.status == 2" type="success" size="mini" plain disabled>已通过</el-button>
            <el-button v-if="scope.row.status == 3" type="info" size="mini" plain disabled>已拒绝</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination :page-size="10" :current-page="page" layout="prev, pager, next"
                     :total="searchList.Count" @current-change="pnChange">
      </el-pagination>
    </div>
  </d2-container>
</template>

<script>
  import { audit_tasklist,userinfo,audit_taskconfirm,auditTask_refuse } from '@/api/api'
  import moment from "moment";
  export default {
    name: "AdminRefundList",
    data(){
      return{
        tableData: [],
        searchList: {
          searchValue: "",
          timelist:[],
          pn: 1,
          Count: 0
        },
        page:1,
        loading:false,
        statusVal:0,
        statuslist: [
          {
            value: 0,
            label: '全部'
          },
          {
            value: 1,
            label: '等待审核'
          }, {
            value: 2,
            label: '审核通过'
          }, {
            value: 3,
            label: '拒绝审核'
          }],
      }
    },
    mounted(){
      let params = {
        page:this.page,
        page_size:20,
      }
      this.getorderList(params)
    },
    methods:{
      //获取用户信息
      getUser(){
        userinfo().then(res=>{
          if(res.Status == 200){
            this.$emit('getMoney',res.Data.money)
          }else{
            this.$message.error(res.Message)
          }
        })
      },
      //分页
      pnChange(pn) {
        this.page = pn
        let params = {
          order:this.searchList.searchValue,
          time:this.searchList.timelist,
          page:pn,
          page_size:10,
          status:this.statusVal
        }
        this.getorderList(params)
      },
      //获取订单列表
      getorderList(params){
        audit_tasklist(params).then(res=>{
          this.searchList = res
          if(res.Status == 200){
            this.tableData = res.Data
            let ipos
            this.tableData.forEach(item => {
              ipos = item.create_time.indexOf("+")
              item.create_time = item.create_time.replace(/T/g," ").substring(0,ipos)
            })
          }else{
            this.$message.error(res.Message)
          }
        })
      },
      //查询
      init(){
        if(this.searchList.timelist.length>0){
          this.searchList.timelist[0] = moment(this.searchList.timelist[0]).format('YYYY-MM-DD HH:mm:ss');
          this.searchList.timelist[1] = moment(this.searchList.timelist[1]).format('YYYY-MM-DD HH:mm:ss');
        }
        let params = {
          order:this.searchList.searchValue,
          time:this.searchList.timelist,
          page:this.page,
          page_size:10,
          status:this.statusVal
        }
        this.getorderList(params)
      },
      //通过
      onPass(row,idx){
        let params = {
          order:row.task_order,
          task_id:row.id,
          address:sessionStorage.getItem('ip'),
        }
        if(idx === 1){
          audit_taskconfirm(params).then(res=>{
            if(res.Status == 200){
              this.$message({
                type: '通过审核',
                message: res.Message
              });
              if(this.page == 1){
                let param = {
                  page:1,
                  page_size:20,
                }
                this.getorderList(param)
              }else{
                let param = {
                  page:this.page,
                  page_size:20,
                }
                this.getorderList(param)
              }
            }else{
              this.$message({
                type: 'error',
                message: res.Message
              });
            }
          })
        }else{
          this.open(params)
        }
      },
      //拒绝
      open(params) {
        this.$prompt('请输入拒绝原因', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
        }).then(({ value }) => {
          params.note = value
          auditTask_refuse(params).then(res=>{
            if(res.Status == 200){
              this.$message({
                type: '拒绝审核',
                message: res.Message
              });
              this.init()
            }else{
              this.$message({
                type: 'error',
                message: res.Message
              });
            }
          })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '取消输入'
          });
        });
      }
    }
  }
</script>

<style scoped>

</style>
